package com.fujin.gulimall.member.controller;

import java.util.Arrays;
import java.util.Map;

import com.fujin.common.Vo.SocialUser;
import com.fujin.common.exception.BizCodeEnume;
import com.fujin.gulimall.member.Exception.PhoneExistException;
import com.fujin.gulimall.member.Exception.UserNameExistException;
import com.fujin.gulimall.member.Vo.RegisterVo;
import com.fujin.gulimall.member.Vo.loginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.fujin.gulimall.member.entity.MemberEntity;
import com.fujin.gulimall.member.service.MemberService;
import com.fujin.common.utils.PageUtils;
import com.fujin.common.utils.R;



/**
 * 会员
 *
 * @author fujin
 * @email 1242257@gmail.com
 * @date 2021-12-25 15:59:30
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    /**
     * 微博社交登录
     * 根据uid判断用户是否第一次登录
     * 第一次登录的话为注册、不是第一次就直接登录
     * @param socialUser
     * @return
     */
    @PostMapping("/auth/login")
    public R login(@RequestBody SocialUser socialUser){
        MemberEntity memberEntity = memberService.login(socialUser);
        if (memberEntity!=null){
            return R.ok().put("memberEntity",memberEntity);
        }else {
            return R.error();
        }
    }

    /**
     * 会员注册
     * @param registerVo
     * @return
     */
    @PostMapping("/regist")
    public R regist(@RequestBody RegisterVo registerVo){
        try {
            memberService.regist(registerVo);
        } catch (PhoneExistException e) {
            R.error(BizCodeEnume.PHONE_EXIST_EXCEPTION.getCode(),BizCodeEnume.PHONE_EXIST_EXCEPTION.getMsg());
        }catch (UserNameExistException e) {
            R.error(BizCodeEnume.USER_EXIST_EXCEPTION.getCode(),BizCodeEnume.USER_EXIST_EXCEPTION.getMsg());
        }
        return R.ok();
    }

    /**
     * 会员登录
     * @param loginVo
     * @return
     */
    @PostMapping("/login")
    public R login(@RequestBody loginVo loginVo){
        MemberEntity memberEntity = memberService.login(loginVo);
        if (memberEntity == null){
            //等于null，说明此次登录有误
            return R.error(BizCodeEnume.LOGINACCT_PASSWORD_EXCEPTION.getCode(),BizCodeEnume.LOGINACCT_PASSWORD_EXCEPTION.getMsg());
        }else {
            return R.ok();
        }
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("member:member:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("member:member:info")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("member:member:save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("member:member:update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
